//
//  HouseInfoGrabRootDataSource.m
//  Jyxt
//
//  Created by 刘日 on 17/5/17.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "HouseInfoGrabRootDataSource.h"
#import "FormInputTableViewCell.h"
#import "PhotosTableViewCell.h"
#import "HouseInfoGrabViewModel.h"
#import "OwnerItemTableViewCell.h"
#import "OwnerInfoModel.h"
#import "LemonBestView.h"

@implementation HouseInfoGrabRootDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section < 2 ? [@[@1, @4][section] integerValue] : [HouseInfoGrabViewModel owners].count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *names = @[@"PhotosTableViewCell",@"FormInputTableViewCell"];
    NSArray *ownerName = @[@"owner_item" , @"owner_new"];
    UITableViewCell *cell;
    if (indexPath.section < 2)
        cell = [tableView dequeueReusableCellWithIdentifier: names[indexPath.section]];
    else
        cell = [tableView dequeueReusableCellWithIdentifier: ownerName[(indexPath.row < [HouseInfoGrabViewModel owners].count) ? 0 : 1]];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed: names[indexPath.section] owner: self options: nil] lastObject];
    }
    if (indexPath.section == 0) {
        ((PhotosTableViewCell *) cell).tableView = tableView;
        ((PhotosTableViewCell *) cell).titleLabel.text = @"添加房产证照片";
    }
    else if (indexPath.section == 1){
        FormInputTableViewCell *inputCell = (FormInputTableViewCell *)cell;
        [inputCell setData: @[@"产权类型" , @"产权编号" , @"详细地址" , @"建筑面积"][indexPath.row] saveKeyPath: @[@"house_type" , @"house_no" , @"house_address" , @"house_area"][indexPath.row]];
        if (indexPath.row == 0){
            // 产权类型选择
            inputCell.inputTextField.userInteractionEnabled = NO;
            inputCell.inputTextField.placeholder = @"请选择";
            NSArray *typesNameArr = @[@"产权", @"使用权" , @"不动产登记", @"其他"];
            inputCell.onTap = ^(UIView *v){
                LemonBestView *as = [LemonBestView defaultActionSheetView];
                [as clear];
                as.headViewHeight = 44;
                as.title =  @"请您选择产权类型";
                LemonBestItem *cancel = [[LemonBestItem alloc] initWithTitle: @"取消" onTouchBlock:^(LemonBestItem *item) {
                    [as hide];
                }];
                ((UILabel *)cancel.contentView).textColor = [UIColor redColor];
                [as addAction: cancel groupIndex: 0];
                [as addActionGroup];
                as.backViewControllerScale = 0.9;// 设置1 就没有后边viewController的缩放效果了
                for (NSInteger i = typesNameArr.count - 1; i >= 0;  i --) {
                    [as addAction: [[LemonBestItem alloc] initWithTitle: typesNameArr[i] onTouchBlock:^(LemonBestItem *item) {
                        inputCell.inputTextField.text = typesNameArr[i];
                        inputCell.inputTextField.tag = i + 1;
                        [as hide];
                    }] groupIndex:1];
                }
                [as show];
            };
        }
    }
    else if (indexPath.section == 2){
        // 添加共有人
        if (indexPath.row < [HouseInfoGrabViewModel owners].count){
            OwnerItemTableViewCell *owner = (OwnerItemTableViewCell *)cell;
            OwnerInfoModel *ownerItem = [HouseInfoGrabViewModel owners][indexPath.row];
            owner.nameLabel.text = [NSString stringWithFormat: @"姓名 %@" , ownerItem.name];
            owner.idLabel.text = [NSString stringWithFormat: @"%@： %@" ,@[@"身份证", @"军人身份证件" , @"武装警察身份证件", @"港澳居民来往内地通行证" , @"台湾居民来往内地通行证" , @"护照"][ownerItem.idType.integerValue - 1], ownerItem.idNo];
        }
            
    }
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2 && indexPath.row < [HouseInfoGrabViewModel owners].count)
        return YES;
    return NO;
}

@end
